﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

using System.Reflection;
using System.IO;
//using System.Web.Mvc;
using System.Data;
using System.Text;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
//using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Html;

namespace 省市级联.Controllers
{
    public static class BusinessLogic
    {

        public static string GetPro(string ProID, string NameAttr = "ProviceID")
        {
            XmlDocument Xml = new XmlDocument();
            StringBuilder sb = new StringBuilder();
            string path = Path.GetFullPath("wwwroot/XmlData/Provinces.xml");

            //var a = Assembly.Load("省市级联.Controllers");
            //Stream sm = a.GetManifestResourceStream("XmlData.Navigation.xml");
            //XmlReaderSettings settings = new XmlReaderSettings();
            //settings.IgnoreComments = true; //忽略注释
            //XmlReader reader = XmlReader.Create(sm, settings);

            Xml.Load(path);
            //   XmlNode ProvincesNode = Xml.SelectSingleNode("Provinces");
            XmlNodeList ProvinceNodeList = Xml.SelectNodes("//Province");
            StringBuilder strCity = new StringBuilder();
            sb.Append("<select id='pro' name='" + NameAttr + "' onchange='ProChange(this)'>>");
            foreach (XmlNode node in ProvinceNodeList)
            {
                if (node.Attributes["ID"].Value == ProID)
                {
                    sb.Append("<option selected='selected' value='" + node.Attributes["ID"].Value + "'>" + node.Attributes["ProvinceName"].Value + "</option>");
                }
                else { sb.Append("<option value='" + node.Attributes["ID"].Value + "'>" + node.Attributes["ProvinceName"].Value + "</option>"); }

            }
            sb.Append("</select>");
            return sb.ToString();
        }
        public static string GetCity(string ProID, string CityID = null, string NameAttr = "CityID")
        {
            XmlDocument Xml = new XmlDocument();

            string path = Path.GetFullPath("wwwroot/XmlData/Cities.xml");
            Xml.Load(path);
            //   XmlNode ProvincesNode = Xml.SelectSingleNode("Provinces");
            XmlNodeList CityNodeList = Xml.SelectNodes("//City[@PID='" + ProID + "']");
            StringBuilder strCity = new StringBuilder();
            strCity.Append("<select id='city' name='" + NameAttr + "' onchange='CityChange(this)'>");
            foreach (XmlNode node in CityNodeList)
            {
                if (node.Attributes["ID"].Value == CityID)
                {
                    strCity.Append("<option selected='selected' value='" + node.Attributes["ID"].Value + "'>" + node.Attributes["CityName"].Value + "</option>");
                }
                else
                {
                    strCity.Append("<option value='" + node.Attributes["ID"].Value + "'>" + node.Attributes["CityName"].Value + "</option>");
                }

            }
            strCity.Append("/<select>");
            return strCity.ToString();
        }

        public static string GetDis(string CityID, string DisID = null, string NameAttr = "DistrictID")
        {
            XmlDocument Xml = new XmlDocument();

            string path = Path.GetFullPath("wwwroot/XmlData/Districts.xml");
            Xml.Load(path);
            //   XmlNode ProvincesNode = Xml.SelectSingleNode("Provinces");
            XmlNodeList DisNodeList = Xml.SelectNodes("//District[@PID='" + CityID + "']");
            StringBuilder strCity = new StringBuilder();
            strCity.Append("<select id='dis' name='" + NameAttr + "'>");
            foreach (XmlNode node in DisNodeList)
            {
                if (node.Attributes["ID"].Value == DisID)
                {
                    strCity.Append("<option selected='selected' value='" + node.Attributes["ID"].Value + "'>" + node.Attributes["DistrictName"].Value + "</option>");

                }
                else { strCity.Append("<option value='" + node.Attributes["ID"].Value + "'>" + node.Attributes["DistrictName"].Value + "</option>"); }

            }
            strCity.Append("/<select>");
            return strCity.ToString();
        }

        public static IHtmlContent GetAddress<TModel, TProperty>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ProvinceID, Expression<Func<TModel, TProperty>> CityID, Expression<Func<TModel, TProperty>> DistrictID, Object HtmlAttribute)
        {
            StringBuilder sb = new StringBuilder();
            string ProName = string.Empty; string CityName = string.Empty; string DisName = string.Empty;
            string ProValue = GetLamdaValue<TModel, TProperty>(htmlHelper, ProvinceID, out ProName);
            string CityValue = GetLamdaValue<TModel, TProperty>(htmlHelper, CityID, out CityName);
            string DisValue = GetLamdaValue<TModel, TProperty>(htmlHelper, DistrictID, out DisName);

            sb.Append(GetPro(ProValue, ProName));
            sb.Append(GetCity(ProValue, CityValue, CityName));
            sb.Append(GetDis(CityValue, DisValue, DisName));
            return new HtmlString(sb.ToString());
        }

        public static string GetLamdaValue<TModel, TProperty>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ID, out string name)
        {
            string lambda = ID.Body.ToString();//形如Model.ProviceID
            name = "";
            string value = string.Empty;
            if (htmlHelper.ViewData.Model != null)
            {

                //value = ModelMetadata.FromLambdaExpression(ID, htmlHelper.ViewData).Model.ToString();
                //  name = ModelMetadata.FromLambdaExpression(ID, htmlHelper.ViewData).PropertyName.ToString();
                // Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata
                //var s = htmlHelper.ViewData.Model.GetType().GetProperty("ProviceID").;
               // var c = s.GetValue(s, null);
               
            }

            return value;
        }
    }
}